package com.fescoservice.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.fescoservice.exception.FSException;
import com.fescoservice.model.Application;
import com.fescoservice.model.Customer;

/**
 * A data access object (DAO) providing persistence and search support for
 * Application entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.fescoservice.dao.Application
 * @author MyEclipse Persistence Tools
 */

public class ApplicationDAO extends HibernateDaoSupport {
	private static final Logger log = LogManager.getLogger(ApplicationDAO.class);
	// property constants
	public static final String CUST_ID = "custId";
	public static final String COMPANY_ID = "companyId";
	public static final String DEPT_ID = "deptId";
	public static final String APPLY_TYPE = "applyType";
	public static final String APPLY_KEY = "applyKey";
	public static final String FEE = "fee";
	public static final String SERVICE_COST = "serviceCost";
	public static final String FINIFH_DATE = "finifhDate";
	public static final String PRE_DATE = "preDate";
	public static final String PAY_DATE = "payDate";
	public static final String HR_ID = "hrId";
	public static final String CREATOR_ID = "creatorId";
	public static final String SERVICE_NUMBER = "serviceNumber";
	public static final String MEMO = "memo";
	public static final String CERT_NUMBER = "certNumber";
	public static final String VALID_DATE = "validDate";
	public static final String BODY_CHECK_DATE = "bodyCheckDate";
	public static final String DRIVE_TYPE = "driveType";
	public static final String STATUS = "status";
	public static final String FEE_STATUS = "feeStatus";
	public static final String LAST_MODIFY_TIME = "lastModifyTime";

	protected void initDao() {
		// do nothing
	}

	public void save(Application transientInstance) {
		log.debug("saving Application instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Application persistentInstance) {
		log.debug("deleting Application instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Application findById(Integer id) {
		log.debug("getting Application instance with id: " + id);
		try {
			Application instance = (Application) getHibernateTemplate().get(
					"com.fescoservice.model.Application", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Application instance) {
		log.debug("finding Application instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Application instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Application as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCustId(Integer custId) {
		return findByProperty(CUST_ID, custId);
	}

	public List findByCompanyId(Integer companyId) {
		return findByProperty(COMPANY_ID, companyId);
	}

	public List findByDeptId(Object deptId) {
		return findByProperty(DEPT_ID, deptId);
	}

	public List findByApplyType(Object applyType) {
		return findByProperty(APPLY_TYPE, applyType);
	}

	public List findByApplyKey(Object applyKey) {
		return findByProperty(APPLY_KEY, applyKey);
	}

	public List findByFee(Object fee) {
		return findByProperty(FEE, fee);
	}

	public List findByServiceCost(Object serviceCost) {
		return findByProperty(SERVICE_COST, serviceCost);
	}

	public List findByFinifhDate(Object finifhDate) {
		return findByProperty(FINIFH_DATE, finifhDate);
	}

	public List findByPreDate(Object preDate) {
		return findByProperty(PRE_DATE, preDate);
	}

	public List findByPayDate(Object payDate) {
		return findByProperty(PAY_DATE, payDate);
	}

	public List findByHrId(Object hrId) {
		return findByProperty(HR_ID, hrId);
	}

	public List findByCreatorId(Object creatorId) {
		return findByProperty(CREATOR_ID, creatorId);
	}

	public List findByServiceNumber(Object serviceNumber) {
		return findByProperty(SERVICE_NUMBER, serviceNumber);
	}

	public List findByMemo(Object memo) {
		return findByProperty(MEMO, memo);
	}

	public List findByCertNumber(Object certNumber) {
		return findByProperty(CERT_NUMBER, certNumber);
	}

	public List findByValidDate(Object validDate) {
		return findByProperty(VALID_DATE, validDate);
	}

	public List findByBodyCheckDate(Object bodyCheckDate) {
		return findByProperty(BODY_CHECK_DATE, bodyCheckDate);
	}

	public List findByDriveType(Object driveType) {
		return findByProperty(DRIVE_TYPE, driveType);
	}

	public List findAll() {
		log.debug("finding all Application instances");
		try {
			String queryString = "from Application";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Application merge(Application detachedInstance) {
		log.debug("merging Application instance");
		try {
			Application result = (Application) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Application instance) {
		log.debug("attaching dirty Application instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Application instance) {
		log.debug("attaching clean Application instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ApplicationDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (ApplicationDAO) ctx.getBean("ApplicationDAO");
	}
	
	
	
	public void updateApplication(final String hql, final Application application)  throws FSException{
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws SQLException,
					HibernateException {
					Query q = session.createQuery(hql);
						q.setString("applyKey" , application.getApplyKey()); 	
						q.setString("finifhDate" ,application.getFinifhDate()); 
						q.setInteger("fee" , application.getFee()); 
						q.setInteger("serviceCost" ,application.getServiceCost()); 
						q.setString("preDate" , application.getPreDate()); 
						q.setString("payDate" , application.getPayDate()); 	
						q.setString("hrId" , application.getHrId()); 
						q.setString("serviceNumber" , application.getServiceNumber()); 
						q.setString("applyType" , application.getApplyType()); 
						q.setString("status" , application.getStatus()); 
						q.setString("feeStatus" , application.getFeeStatus()); 
						q.setString("applyArea" , application.getApplyArea()); 
						q.setString("memo" , application.getMemo()); 
						q.setInteger("applyId" , application.getApplyId()); 
						System.out.println("L265:-------------"+q.getQueryString());
					q.executeUpdate();
				return null;
			}
		});

	}
	
	public void delete(final String hql, final Application application)  throws FSException{
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws SQLException,
					HibernateException {
						Query q = session.createQuery(hql);
						q.setInteger("applyId" , application.getApplyId()); 
						q.executeUpdate();
				return null;
			}
		});

	}
	
	
	
}